<!DOCTYPE html>
<html dir="ltr">

<head>
  <meta charset="UTF-8">
  <title>Infinite Scroll</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <script src="/dist/ionic.js"></script>
</head>

<body>
  <ion-app>


      <ion-header>
        <ion-toolbar>
          <ion-title>Infinite Scroll</ion-title>
        </ion-toolbar>
      </ion-header>

      <ion-content id="content" padding>

        <ion-button onclick="toggleInfiniteScroll()" block>
          Toggle InfiniteScroll
        </ion-button>

        <ion-list id="list">

        </ion-list>

        <ion-infinite-scroll threshold="100px" id="infinite-scroll">
          <ion-infinite-scroll-content
            loadingSpinner="bubbles"
            loadingText="Loading more data...">
          </ion-infinite-scroll-content>
        </ion-infinite-scroll>
      </ion-content>



  </ion-app>

  <script>
    let items = [];
    for (var i = 0; i < 30; i++) {
      items.push( i+1 );
    }
    const list = document.getElementById('list');
    const infiniteScroll = document.getElementById('infinite-scroll');

    function toggleInfiniteScroll() {
      infiniteScroll.disabled = !infiniteScroll.disabled;
    }

    infiniteScroll.addEventListener('ionInfinite', async function() {
      console.log('Loading data...');
      const data = await getAsyncData();
      items = items.concat(data);
      infiniteScroll.complete();
      render();
      console.log('Done');
    });

    function render() {
      let html = '';
      for(let item of items) {
        html += `<ion-item>${item}</ion-item>`;
      }
      list.innerHTML = html;
    }

    function getAsyncData() {
      // async return mock data
      return new Promise(resolve => {
        setTimeout(() => {
          let data = [];
          for (var i = 0; i < 30; i++) {
            data.push(i);
          }

          resolve(data);
        }, 500);
      });
    }
    render();

  </script>
</body>
</html>
